const Controller = require('egg').Controller;

class WeightController extends Controller {

    constructor(ctx) {
        super(ctx);
    }

    //上报
    async report() {
        const { app, ctx, service } = this;
        //let nsp = app.io.of('/');
        let latestRow = await service.weight.findLatest(); //最近上报的数据
        let newestRows;
        if(latestRow){
            //从上一次上报的数据的下一条开始继续上报
            newestRows = await service.avs.findLatest(latestRow.tareTime, latestRow.no);
        }
        else{
            //未曾上报过数据
            newestRows = await service.avs.findLatest('', '');
        }

        if(newestRows && newestRows.length > 0){
            let host = app.config.avs.host;
            let url = host + "/device/data/acs/czjg/weight/addList";

            let ids  = [];
            let data = [];
            let params = [];
            for(let item of newestRows){
                let time = Math.floor((new Date(item.created_at)).getTime()/1000);
                if(item.datastatus == 9 || item.datastatus == '9'){
                    continue;
                }
                else if(!item.gross || !item.tare){
                    continue;
                }
                let timestamp1 = (new Date(item.grossdatetime)).getTime() - 8 * 60 * 60 * 1000;
                let timestamp2 = (new Date(item.taredatetime)).getTime() - 8 * 60 * 60 * 1000;
                let term_id, systemCode, disposeUnitCode, weightNo, rate;
                if(item.product.indexOf('餐厨垃圾') !== -1){
                    term_id = "864";
                    systemCode = "WZYTH";
                    disposeUnitCode = "18";
                    weightNo = "WEIGH864";
                }
                else if(item.product.indexOf('其他垃圾') !== -1 || item.product.indexOf('陈腐垃圾') !== -1 || item.product.indexOf('生活垃圾') !== -1 || item.product.indexOf('厨余垃圾') !== -1 || item.product.indexOf('固化飞灰') !== -1){
                    term_id = "820";
                    systemCode = "WZYTH";
                    disposeUnitCode = "17";
                    weightNo = "WEIGH820";
                }
                else{
                    continue;
                }

                if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                    rate = 9;   //9:1
                }
                else if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('餐厨垃圾') !== -1){
                    rate = 10;//10:0
                }
                else if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('厨余垃圾') !== -1){
                    rate = 10;//10:0
                }
                else if(item.sender.indexOf('文成垃圾填埋场') !== -1 && item.product.indexOf('陈腐垃圾') !== -1){
                    rate = 9;//9:1
                }
                else if(item.sender.indexOf('文成伟明环保能源有限公司') !== -1 && item.product.indexOf('固化飞灰') !== -1){
                    rate = 9;//9:1
                }
                else if(item.sender.indexOf('泰顺') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                    rate = 6; //6:4
                }
                else if(item.sender.indexOf('苍南') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                    rate = 6; //6:4
                }
                else if(item.sender.indexOf('龙港') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                    rate = 10; //10:0
                }
                else{
                    rate = 9; //9:1
                }

                //重传十月份三种车牌的垃圾
                if(item.product.indexOf('厨余垃圾') !== -1 && (item.truckno == '浙CH0936' || item.truckno == '浙CM0767' || item.truckno == '浙CM8960') && item.id <= 35304){
                    item.product = '其他垃圾';
                    rate = 9;
                }

                data.push({
                    term_id: term_id,
                    systemCode: systemCode,
                    disposeUnitCode: disposeUnitCode,
                    weightNo: weightNo,
                    no: item.id,
                    carNo: item.truckno,
                    grossTime: (new Date()).setTime(timestamp1),
                    tareTime: (new Date()).setTime(timestamp2),
                    grossWeight: item.gross,
                    tareWeight: item.tare,
                    netWeight: item.net,
                    productName: item.product,
                    icCode: item.cardno,
                    sourceUnit: item.sender,
                    transportUnit: item.transporter,
                    handleUnit: item.receiver,
                    memo: '',
                    str1: rate,
                    area: '文成县',
                    status: 1
                });

                params.push({
                    report_id: term_id + systemCode + disposeUnitCode + weightNo + item.id,
                    term_id: term_id,
                    systemCode: systemCode,
                    disposeUnitCode: disposeUnitCode,
                    weightNo: weightNo,
                    no: item.id,
                    carNo: item.truckno,
                    grossTime: (new Date()).setTime(timestamp1),
                    tareTime: (new Date()).setTime(timestamp2),
                    grossWeight: item.gross,
                    tareWeight: item.tare,
                    netWeight: item.net,
                    productName: item.product,
                    icCode: item.cardno,
                    sourceUnit: item.sender,
                    transportUnit: item.transporter,
                    handleUnit: item.receiver,
                    memo: '',
                    str1: rate,
                    status: 1
                });
                ids.push(item.id);
            }

            let rtn = await service.weight.insertMany(params);
            if(rtn && rtn.length > 0){
                let options = {
                    dataType: 'json',
                    method: 'POST',
                    data: data,
                    headers: {
                        "Content-Type": "application/json; charset=utf-8"
                    }
                };
                const res = await ctx.curl(url, options);
                ctx.logger.warn('00000000000000000');
                ctx.logger.warn(res);
                if(res && res.status == 200 && res.data && res.data.rc == 0){
                    let resUpdate = await service.weight.updateFlag(ids, 'success');
                    if(resUpdate && resUpdate.nModified >= 1){
                        //nsp.emit('newcoming', {
                            //type: 'success',
                            //msg: 'report success'
                        //});
                        ctx.helper.success(ctx, '上报成功，状态更新成功', res.data.ret);
                    }
                    else{
                        //nsp.emit('newcoming', {
                            //type: 'fail',
                            //msg: 'report fail'
                        //});
                        ctx.helper.success(ctx, '上报成功，状态更新失败', res.data.ret);
                    }
                }
                else{
                    await service.weight.updateFlag(ids, 'fail');
                    ctx.helper.fail(ctx, '上报失败', res);
                }
            }
            else{
                ctx.helper.fail(ctx, '数据插入失败', '');
            }
        }
        else{
            ctx.helper.fail(ctx, '没有数据', '');
        }
    }

    //上报失败的数据重新上报
    async retry() {
        const { app, ctx, service } = this;
        //let nsp = app.io.of('/');
        let lists = await service.weight.findFailList();
        if(lists && lists.length > 0){
            let host = app.config.avs.host;
            let url = host + "/device/data/acs/czjg/weight/addList";

            let ids  = [];
            let data = [];
            for(let item of lists){
                data.push({
                    term_id: item.term_id,
                    systemCode: item.systemCode,
                    disposeUnitCode: item.disposeUnitCode,
                    weightNo: item.weightNo,
                    no: item.no,
                    carNo: item.carNo,
                    grossTime: item.grossTime,
                    tareTime: item.tareTime,
                    grossWeight: item.grossWeight,
                    tareWeight: item.tareWeight,
                    netWeight: item.netWeight,
                    productName: item.productName,
                    icCode: item.icCode,
                    sourceUnit: item.sourceUnit,
                    transportUnit: item.transportUnit,
                    handleUnit: item.handleUnit,
                    status: item.status,
                    area: '文成县',
                    str1: item.str1,
                    memo: item.memo
                });
                ids.push(item.no);
            }

            let options = {
                dataType: 'json',
                method: 'POST',
                data: data,
                headers: {
                    "Content-Type": "application/json; charset=utf-8"
                }
            };
            const res = await ctx.curl(url, options);
            ctx.logger.warn('2222222222222222');
            ctx.logger.warn(res);
            if(res && res.status == 200 && res.data && res.data.rc == 0){
                let resUpdate = await service.weight.updateFlag(ids, 'success');
                if(resUpdate && resUpdate.nModified >= 1){
                    //nsp.emit('newcoming', {
                        //type: 'success',
                        //msg: 'report success'
                    //});
                    ctx.helper.success(ctx, '上报成功，状态更新成功', res.data.ret);
                }
                else{
                    //nsp.emit('newcoming', {
                        //type: 'fail',
                        //msg: 'report fail'
                    //});
                    ctx.helper.success(ctx, '上报成功，状态更新失败', res.data.ret);
                }
            }
            else{
                ctx.helper.fail(ctx, '上报失败', res);
            }
        }
        else{
            ctx.helper.fail(ctx, '没有数据', '');
        }
    }

    //检查昨天的数据，如有删除，更新删除状态
    async check() {
        return false;
        const { app, ctx, service } = this;
        let reportLists = await service.weight.findYesterdayList();
        if(reportLists && reportLists.length > 0){
            let originalLists = await service.avs.findYesterdayList();
            if(originalLists && originalLists.length > 0){
                let deleteLists = [];
                for(let item1 of reportLists){
                    let flag = false;
                    for(let item2 of originalLists){
                        let reportStatus = Number(item1.status);
                        let originalStatus = Number(item2.datastatus);
                        if(item1.no == item2.id && reportStatus != originalStatus && originalStatus == 9){
                            flag = true;
                            break;
                        }
                    }

                    if(flag){
                        item1.status = 0;
                        deleteLists.push(item1);
                    }
                }

                if(deleteLists.length > 0){
                    let host = app.config.avs.host;
                    let url = host + "/device/data/acs/czjg/weight/delete";

                    let ids  = [];
                    let data = [];
                    for(let item of deleteLists){
                        let options = {
                            dataType: 'json',
                            method: 'POST',
                            data: {
                                term_id: item.term_id,
                                systemCode: item.systemCode,
                                disposeUnitCode: item.disposeUnitCode,
                                weightNo: item.weightNo,
                                no: item.no,
                            },
                            headers: {
                                "Content-Type": "application/json; charset=utf-8"
                            }
                        };
                        const res = await ctx.curl(url, options);
                        if(res && res.status == 200 && res.data && res.data.rc == 0){
                            ids.push(item.no);
                        }
                    }

                    if(ids.length > 0){
                        let resUpdate = await service.weight.deleteByIds(ids);
                        if(resUpdate && resUpdate[0]){
                            ctx.helper.success(ctx, '状态更新成功', res.data.ret);
                        }
                        else{
                            ctx.helper.fail(ctx, '修改数据失败', '');
                        }
                    }
                    else{
                        ctx.helper.fail(ctx, '没有数据1', '');
                    }
                }
                else{
                    ctx.helper.fail(ctx, '没有数据2', '');
                }
            }
            else{
                ctx.helper.fail(ctx, '没有数据3', '');
            }
        }
        else{
            ctx.helper.fail(ctx, '没有数据4', '');
        }
    }

    //检查昨天的数据，如有修改，更新状态
    async edit() {
        return false;
       const { app, ctx, service } = this;
       let reportLists = await service.weight.findYesterdayList();
       if(reportLists && reportLists.length > 0){
           let originalLists = await service.avs.findYesterdayList();
           if(originalLists && originalLists.length > 0){
               let updateLists = [];
               for(let item1 of reportLists){
                   let flag = false;
                   for(let item2 of originalLists){
                       let reportStatus = Number(item1.status);
                       let originalStatus = Number(item2.datastatus);
                       if(item1.no == item2.id && originalStatus != 9){
                           if(item1.carNo != item2.truckno || item1.netWeight != item2.net){
                               //如果有修改数据，重新上报
                               flag = true;
                               item1.carNo = item2.truckno;
                               item1.netWeight = item2.net;
                               item1.sourceUnit = item2.sender;
                               item1.transportUnit = item2.transporter;
                               item1.grossWeight = item2.gross;
                               item1.tareWeight = item2.tare;
                               break;
                           }
                       }
                   }

                   if(flag){
                       updateLists.push(item1);
                   }
               }

               if(updateLists.length > 0){
                   let host = app.config.avs.host;
                   let url = host + "/device/data/acs/czjg/weight/update";

                   let ids  = [];
                   let data = [];
                   for(let item of updateLists){
                       let options = {
                           dataType: 'json',
                           method: 'POST',
                           data: {
                               term_id: item.term_id,
                               systemCode: item.systemCode,
                               disposeUnitCode: item.disposeUnitCode,
                               weightNo: item.weightNo,
                               no: item.no,
                               carNo: item.carNo,
                               grossTime: item.grossTime,
                               tareTime: item.tareTime,
                               grossWeight: item.grossWeight,
                               tareWeight: item.tareWeight,
                               netWeight: item.netWeight,
                               productName: item.productName,
                               icCode: item.icCode,
                               sourceUnit: item.sourceUnit,
                               transportUnit: item.transportUnit,
                               handleUnit: item.handleUnit,
                               area: '文成县',
                               str1: item.str1,
                               memo: item.memo
                           },
                           headers: {
                               "Content-Type": "application/json; charset=utf-8"
                           }
                       };
                       const res = await ctx.curl(url, options);
                       if(res && res.status == 200 && res.data && res.data.rc == 0){
                           ids.push(item.no);
                       }
                   }

                   if(ids.length > 0){
                       let resUpdate = await service.weight.updateFlag(ids, 'success');
                       if(resUpdate && resUpdate.nModified >= 1){
                           //nsp.emit('newcoming', {
                               //type: 'success',
                               //msg: 'report success'
                           //});
                           ctx.helper.success(ctx, '上报成功，状态更新成功', res.data.ret);
                       }
                       else{
                           //nsp.emit('newcoming', {
                               //type: 'fail',
                               //msg: 'report fail'
                           //});
                           ctx.helper.success(ctx, '上报成功，状态更新失败', res.data.ret);
                       }
                   }
                   else{
                       ctx.helper.fail(ctx, '没有数据', '');
                   }
               }
               else{
                   ctx.helper.fail(ctx, '没有数据', '');
               }
           }
           else{
               ctx.helper.fail(ctx, '没有数据', '');
           }
       }
       else{
           ctx.helper.fail(ctx, '没有数据', '');
       }
    }

    //重新核对两天以前的数据，有漏上报的则上报
    async again() {
        return false;
        const { app, ctx, service } = this;
        //获取两天以前的数据
        let originalRows = await service.avs.findTwodaysBefore();
        if(originalRows && originalRows.length > 0){
            let ids = [];
            for(let item of originalRows){
                ids.push(item.id);
            }

            let reportRows = await service.weight.findInIds(ids);
            let failLists = [];
            for(let item1 of originalRows){
                let flag = false;
                for(let item2 of reportRows){
                    if(item2.no == item1.id){
                        flag = true;
                        break;
                    }
                }

                if(!flag){
                    failLists.push(item1);
                }
            }

            if(failLists && failLists.length > 0){
                let host = app.config.avs.host;
                let url = host + "/device/data/acs/czjg/weight/addList";
                let ids  = [];
                let data = [];
                let params = [];
                for(let item of failLists){
                    if(item.datastatus == 9 || item.datastatus == '9'){
                        continue;
                    }
                    else if(!item.gross || !item.tare){
                        continue;
                    }

                    let timestamp1 = (new Date(item.grossdatetime)).getTime() - 8 * 60 * 60 * 1000;
                    let timestamp2 = (new Date(item.taredatetime)).getTime() - 8 * 60 * 60 * 1000;
                    let term_id, systemCode, disposeUnitCode, weightNo, rate;
                    if(item.product.indexOf('餐厨垃圾') !== -1){
                        term_id = "864";
                        systemCode = "WZYTH";
                        disposeUnitCode = "18";
                        weightNo = "WEIGH864";
                    }
                    else if(item.product.indexOf('其他垃圾') !== -1 || item.product.indexOf('陈腐垃圾') !== -1 || item.product.indexOf('生活垃圾') !== -1 || item.product.indexOf('厨余垃圾') !== -1 || item.product.indexOf('固化飞灰') !== -1){
                        term_id = "820";
                        systemCode = "WZYTH";
                        disposeUnitCode = "17";
                        weightNo = "WEIGH820";
                    }
                    else{
                        continue;
                    }

                    if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 9;   //9:1
                    }
                    else if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('餐厨垃圾') !== -1){
                        rate = 10;//10:0
                    }
                    else if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('厨余垃圾') !== -1){
                        rate = 10;//10:0
                    }
                    else if(item.sender.indexOf('文成垃圾填埋场') !== -1 && item.product.indexOf('陈腐垃圾') !== -1){
                        rate = 9;//9:1
                    }
                    else if(item.sender.indexOf('文成伟明环保能源有限公司') !== -1 && item.product.indexOf('固化飞灰') !== -1){
                        rate = 9;//9:1
                    }
                    else if(item.sender.indexOf('泰顺') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 6; //6:4
                    }
                    else if(item.sender.indexOf('苍南') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 6; //6:4
                    }
                    else if(item.sender.indexOf('龙港') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 10; //10:0
                    }
                    else{
                        rate = 9; //9:1
                    }

                    data.push({
                        term_id: term_id,
                        systemCode: systemCode,
                        disposeUnitCode: disposeUnitCode,
                        weightNo: weightNo,
                        no: item.id,
                        carNo: item.truckno,
                        grossTime: (new Date()).setTime(timestamp1),
                        tareTime: (new Date()).setTime(timestamp2),
                        grossWeight: item.gross,
                        tareWeight: item.tare,
                        netWeight: item.net,
                        productName: item.product,
                        icCode: item.cardno,
                        sourceUnit: item.sender,
                        transportUnit: item.transporter,
                        handleUnit: item.receiver,
                        area: '文成县',
                        str1: rate,
                        memo: ''
                    });

                    params.push({
                        report_id: term_id + systemCode + disposeUnitCode + weightNo + item.id,
                        term_id: term_id,
                        systemCode: systemCode,
                        disposeUnitCode: disposeUnitCode,
                        weightNo: weightNo,
                        no: item.id,
                        carNo: item.truckno,
                        grossTime: (new Date()).setTime(timestamp1),
                        tareTime: (new Date()).setTime(timestamp2),
                        grossWeight: item.gross,
                        tareWeight: item.tare,
                        netWeight: item.net,
                        productName: item.product,
                        icCode: item.cardno,
                        sourceUnit: item.sender,
                        transportUnit: item.transporter,
                        handleUnit: item.receiver,
                        memo: '',
                        str1: rate,
                        status: 1
                    });

                    ids.push(item.id);
                }

                if(data.length > 0){
                    let rtn = await service.weight.insertMany(params);
                    if(rtn && rtn.length > 0){
                        let options = {
                            dataType: 'json',
                            method: 'POST',
                            data: data,
                            headers: {
                                "Content-Type": "application/json; charset=utf-8"
                            }
                        };

                        const res = await ctx.curl(url, options);
                        ctx.logger.warn('3333333333333333333333');
                        ctx.logger.warn(res);
                        if(res && res.status == 200 && res.data && res.data.rc == 0){
                            let resUpdate = await service.weight.updateFlag(ids, 'success');
                            if(resUpdate && resUpdate[0]){
                                ctx.helper.success(ctx, '上报成功，状态更新成功', res.data.ret);
                            }
                            else{
                                ctx.helper.success(ctx, '上报成功，状态更新失败', res.data.ret);
                            }
                        }
                        else{
                            await service.weight.updateFlag(ids, 'fail');
                            ctx.helper.fail(ctx, '上报失败', res);
                        }
                    }
                    else{
                        ctx.helper.fail(ctx, '数据插入失败', res);
                    }
                }
                else{
                    ctx.helper.fail(ctx, '没有数据', '');
                }
            }
            else{
                ctx.helper.fail(ctx, '没有数据', '');
            }
        }
        else{
            ctx.helper.fail(ctx, '没有数据', '');
        }
    }

    async index() {
        const { ctx, service } = this;
        // 组装参数
        const payload = ctx.query;
        // 调用 Service 进行业务处理
        //const res = await service.weight.index(payload);
        // 设置响应内容和响应状态码

        let str = '欢迎来到天天的世界！';
        let arr = [{
            name: '明明',
            sex: '男',
            age: '18'
        },{
            name: '璇璇',
            sex: '女',
            age: '16'
        }];

        await this.ctx.render('report', {
            arr,
            str
        });
    }

    async page() {
        const { ctx, service } = this;
        // 组装参数
        const payload = ctx.query || {};
        payload.isPaging = true;
        payload.search = typeof(payload.search) != undefined ? payload.search : '' ;
        // 调用 Service 进行业务处理
        const res = await service.weight.index(payload);
        // 设置响应内容和响应状态码

        ctx.helper.success(ctx, '获取成功', res);
    }

    async delete() {
        const { app, ctx, service } = this;
        const payload = ctx.request.body;
        let row = await service.weight.findByWeightID(payload.no);
        if(row){
            let ids  = [];
            let host = app.config.avs.host;
            let url = host + "/device/data/acs/czjg/weight/delete";
            let param = {
                "term_id": row.term_id,
                "systemCode": row.systemCode,
                "disposeUnitCode": row.disposeUnitCode,
                "weightNo": row.weightNo,
                "no": row.no
            };
            ids.push(row.no);

            let header = {
                "Content-Type": "application/json; charset=utf-8",
            };

            let options = {
                dataType: 'json',
                contentType: 'json',
                method: 'POST',
                data: param,
                header: header
            };
            const res = await ctx.curl(url, options);
            ctx.logger.warn('666666666666666666');
            ctx.logger.warn(res);
            if(res && res.status == 200 && res.data && res.data.rc == 0){
                let resUpdate = await service.weight.deleteByIds(ids);
                if(resUpdate && resUpdate.nModified >= 1){
                    ctx.helper.success(ctx, '上报成功，状态更新成功', res.data.data);
                }
                else{
                    ctx.helper.success(ctx, '上报成功，状态更新失败', res.data.data);
                }
            }
            else{
                ctx.helper.fail(ctx, '上报失败', res);
            }
        }
        else{
            ctx.helper.fail(ctx, '没有数据', '');
        }
    }

    //手动上报
    async manual() {
        const { app, ctx, service } = this;
        const payload = ctx.request.body;
        payload.start = payload && payload.start ? payload.start : ctx.helper.formatTime((new Date()).getTime() -  1.5 * 24 * 60 * 60 * 1000);
        payload.end = payload && payload.end ? payload.end : ctx.helper.formatTime((new Date()).getTime() - 1 * 24 * 60 * 60 * 1000);
        if(payload && payload.start && payload.end){
            let rows = await service.avs.findByRange(payload.start, payload.end);
            if(rows && rows.length > 0){
                let host = app.config.avs.host;
                let url = host + "/device/data/acs/czjg/weight/addList";
                let delurl = host + "/device/data/acs/czjg/weight/delete";
                let ids  = [];
                let data = [];
                let params = [];
                let deleteIds = [];
                let deleteParams = [];
                let uprows = await service.weight.findByRange(payload.start, payload.end);

                //判断已上报数据的是否被删除
                for(let uprow of uprows){
                    let flag1 = false;
                    for(let originalrow of rows){
                        if(uprow.weightId == originalrow.id && originalrow.datastatus != '9'){
                            flag1 = true;
                            break;
                        }
                    }
                    if(!flag1){
                        deleteIds.push(uprow.id);
                        deleteParams.push({
                            term_id: item.term_id,
                            systemCode: item.systemCode,
                            disposeUnitCode: item.disposeUnitCode,
                            weightNo: item.weightNo,
                            no: item.no
                        });
                    }
                }
                for(let item of rows){
                    let time = Math.floor((new Date(item.created_at)).getTime()/1000);
                    if(item.datastatus == 9 || item.datastatus == '9'){
                        continue;
                    }
                    else if(!item.gross || !item.tare){
                        continue;
                    }
                    let timestamp1 = (new Date(item.grossdatetime)).getTime() - 8 * 60 * 60 * 1000;
                    let timestamp2 = (new Date(item.taredatetime)).getTime() - 8 * 60 * 60 * 1000;
                    let term_id, systemCode, disposeUnitCode, weightNo, rate;
                    if(item.product.indexOf('餐厨垃圾') !== -1){
                        term_id = "864";
                        systemCode = "WZYTH";
                        disposeUnitCode = "18";
                        weightNo = "WEIGH864";
                    }
                    else if(item.product.indexOf('其他垃圾') !== -1 || item.product.indexOf('陈腐垃圾') !== -1 || item.product.indexOf('生活垃圾') !== -1 || item.product.indexOf('厨余垃圾') !== -1 || item.product.indexOf('固化飞灰') !== -1){
                        term_id = "820";
                        systemCode = "WZYTH";
                        disposeUnitCode = "17";
                        weightNo = "WEIGH820";
                    }
                    else{
                        continue;
                    }

                    if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 9;   //9:1
                    }
                    else if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('餐厨垃圾') !== -1){
                        rate = 10;//10:0
                    }
                    else if(item.sender.indexOf('文成') !== -1 && item.product.indexOf('厨余垃圾') !== -1){
                        rate = 10;//10:0
                    }
                    else if(item.sender.indexOf('文成垃圾填埋场') !== -1 && item.product.indexOf('陈腐垃圾') !== -1){
                        rate = 9;//9:1
                    }
                    else if(item.sender.indexOf('文成伟明环保能源有限公司') !== -1 && item.product.indexOf('固化飞灰') !== -1){
                        rate = 9;//9:1
                    }
                    else if(item.sender.indexOf('泰顺') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 6; //6:4
                    }
                    else if(item.sender.indexOf('苍南') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 6; //6:4
                    }
                    else if(item.sender.indexOf('龙港') !== -1 && item.product.indexOf('其他垃圾') !== -1){
                        rate = 10; //10:0
                    }
                    else{
                        rate = 9; //9:1
                    }

                    data.push({
                        term_id: term_id,
                        systemCode: systemCode,
                        disposeUnitCode: disposeUnitCode,
                        weightNo: weightNo,
                        no: item.id,
                        carNo: item.truckno,
                        grossTime: (new Date()).setTime(timestamp1),
                        tareTime: (new Date()).setTime(timestamp2),
                        grossWeight: item.gross,
                        tareWeight: item.tare,
                        netWeight: item.net,
                        productName: item.product,
                        icCode: item.cardno,
                        sourceUnit: item.sender,
                        transportUnit: item.transporter,
                        handleUnit: item.receiver,
                        memo: '',
                        str1: rate,
                        area: '文成县',
                        status: 1
                    });

                    let row = await service.weight.findByWeightID(item.id);
                    if(row && row.no){
                        //有记录数据 do nothing
                    }
                    else{
                        //没有记录数据，插入记录数据
                        params.push({
                            report_id: term_id + systemCode + disposeUnitCode + weightNo + item.id,
                            term_id: term_id,
                            systemCode: systemCode,
                            disposeUnitCode: disposeUnitCode,
                            weightNo: weightNo,
                            no: item.id,
                            carNo: item.truckno,
                            grossTime: (new Date()).setTime(timestamp1),
                            tareTime: (new Date()).setTime(timestamp2),
                            grossWeight: item.gross,
                            tareWeight: item.tare,
                            netWeight: item.net,
                            productName: item.product,
                            icCode: item.cardno,
                            sourceUnit: item.sender,
                            transportUnit: item.transporter,
                            handleUnit: item.receiver,
                            memo: '',
                            str1: rate,
                            status: 1
                        });
                    }

                    ids.push(item.id);
                }

                if(data.length > 0){
                    if(params.length > 0){
                        //有未上报的数据插入数据库
                        let insertRtn = await service.weight.insertMany(params);
                    }
                    if(deleteIds.length > 0){
                        //有删除的数据更新上报状态
                        let updateRtn = await service.weight.updateStatus(deleteIds, 0);
                        for(let delItem of deleteParams){
                            let header = {
                                "Content-Type": "application/json",
                            };
                            let optionsDel = {
                                dataType: 'json',
                                method: 'POST',
                                data: delItem,
                                header: header
                            };
                            const resDel = await ctx.curl(delurl, optionsDel);
                            ctx.logger.warn('999999999999999');
                            ctx.logger.warn(resDel);
                        }
                    }
                    let options = {
                        dataType: 'json',
                        method: 'POST',
                        data: data,
                        headers: {
                            "Content-Type": "application/json; charset=utf-8"
                        }
                    };

                    const res = await ctx.curl(url, options);
                    ctx.logger.warn('22222222222222222222');
                    ctx.logger.warn(res);
                    if(res && res.status == 200 && res.data && res.data.rc == 0){
                        let resUpdate = await service.weight.updateFlag(ids, 'success');
                        if(resUpdate && resUpdate.nModified >= 1){
                            ctx.helper.success(ctx, '上报成功，状态更新成功', res.data.result);
                        }
                        else{
                            ctx.helper.success(ctx, '上报成功，状态更新失败', res.data.result);
                        }
                    }
                    else{
                        await service.weight.updateFlag(ids, 'fail');
                        ctx.helper.fail(ctx, '上报失败', res);
                    }
                }
                else{
                    ctx.helper.fail(ctx, '没有数据', '');
                }
            }
            else{
                ctx.helper.fail(ctx, '没有数据', '');
            }
        }
        else{
            ctx.helper.fail(ctx, '参数错误', '');
        }
    }
}

module.exports = WeightController;
